﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AMOOF {
    public partial class FitGraphs : UserControl {

        private Dictionary<int, Panel>pdf_displays = new Dictionary<int,Panel>();
        private bool loaded = false;
        private Color[] colors = new Color[] {Color.Red, Color.Blue, Color.Green, Color.Gold, Color.Gray, Color.Honeydew, 
            Color.GreenYellow, Color.IndianRed, Color.Coral, Color.LawnGreen, Color.Khaki, Color.LavenderBlush,
            Color.LemonChiffon, Color.Maroon, Color.MediumPurple, Color.MediumTurquoise, Color.MediumOrchid, Color.Linen,
            Color.AliceBlue, Color.Aqua, Color.Azure, Color.Beige, Color.Beige, Color.Bisque, Color.BlanchedAlmond,
            Color.BlueViolet, Color.Brown, Color.CadetBlue };

        public FitGraphs() {
            InitializeComponent();
            btnGenerateWordDocument.Text = "Generate Reports";
        }

        private void GeneratePDFs() {
            var reports = FormAMOOF.Instance.GetReports();
            int offset = 0;

            foreach (var kvp in reports) {
                if (kvp.Value.Integral == null) { continue; }
                Panel p = new Panel();
                OnOffButton btn = new OnOffButton();
                Label pdf = new Label();

                string name = kvp.Value.IsMixed ? "Mixed" : "" + kvp.Value.PDF.name.Replace(" ", "");

                p.Name = "pnl" + name;
                btn.Name = "btn" + name;
                pdf.Name = "lbl" + name;

                btn.BackgroundImage = (Bitmap)Properties.Resources.ResourceManager.GetObject("btn_round_blue_off");

                btn.OnImage = "btn_round_blue_on";
                btn.OffImage = "btn_round_blue_off";

                btn.Location = new Point(0, 3);
                btn.Size = new Size(28, 28);

                pdf.Size = new Size(254, 18);
                pdf.Location = new Point(28, 8);
                pdf.Font = new Font("Arial", 12, FontStyle.Regular);
                pdf.Text = kvp.Value.IsMixed ? "Mixed " : "" + kvp.Value.PDF.name;

                p.Tag = offset;

                btn.MouseClick += new MouseEventHandler(PDF_Selected);
                pdf.MouseClick += new MouseEventHandler(PDF_Selected);
                p.MouseClick += new MouseEventHandler(PDF_Selected);

                p.Controls.Add(btn);
                p.Controls.Add(pdf);
                p.Size = new Size(303, 34);
                p.Location = new Point(0, p.Height * offset);
                try {
                    pdf_displays.Add(offset, p);
                }
                catch (Exception) {
                    pdf_displays[offset] = p;
                }
                pnlPDFResults.Controls.Add(p);
                offset++;
            }
        }

        private void PDF_Selected(object sender, EventArgs e) {

            Histogram histo = FormAMOOF.Instance.GetHistogram();
            var reports = FormAMOOF.Instance.GetReports();
            histo.ClearPDFs();
            pnlReportsPreview.Controls.Clear();

            int inc = 0;
            foreach (var kvp in pdf_displays) {
                Report r = FormAMOOF.Instance.GetReport(kvp.Value.Controls["lbl" + kvp.Value.Name.Substring(3)].Text.Replace(" ", String.Empty));
                r.InReport = ((OnOffButton)kvp.Value.Controls["btn" + kvp.Value.Name.Substring(3)]).Selected;
                if (r.InReport) {
                    ReportObject ro = new ReportObject();
                    
                    ro.PDFName.Text = r.PDF.name;
                    ro.Name = r.PDF.name.Replace(" ", String.Empty);
                    ro.Error.Text = r.PDF.AverageError.ToString();
                    histo.ImageSize = ro.Histogram.Size;
                    ro.Histogram.BackgroundImage = histo.GetHistogramBitmap(ro.Histogram.Size, r.PDF);
                    ro.PDFCTEVaR.RowCount = 2;
                    ro.PDFCTEVaR.Rows[0].HeaderCell.Value = "CTE";
                    ro.PDFCTEVaR.Rows[1].HeaderCell.Value = "VaR";
                    int count = 0;
                    int percentile = 1;
                    for (int i = 0; i < 21 ; i++) {
                        ro.PDFCTEVaR.Rows[0].Cells[count].Value = String.Format("{0:0.0000####}", r.Integral.getPercentile(percentile).leftCte);
                        ro.PDFCTEVaR.Rows[1].Cells[count].Value = String.Format("{0:0.0000####}", r.Integral.getPercentile(percentile).var);
                        count++;
                        if (percentile == 1) {
                            percentile = 5;
                        }
                        else if (percentile == 95) {
                            percentile = 99;
                        }
                        else {
                            percentile += 5;
                        }
                    }
                    for (int i = 1; i < 5; i++) {
                        ro.SetMoment(i, r.PDF.Moments[i]);
                        ro.SetMoment(-i, r.PDF.Moments[-i]);
                    }

                    
                    ro.Location = new Point(0, inc * ro.Height);
                    histo.AddPDF(r.PDF, colors[inc]);
                    pnlReportsPreview.Controls.Add(ro);
                    inc++;
                }
            }
            pnlReportsPreview.Refresh();
            histo.ImageSize = pnlDiscreteHistogram.Size;
            pnlDiscreteHistogram.BackgroundImage = histo.GetHistogram();


        }

        private void FitGraphs_Load(object sender, EventArgs e) {
            loaded = true;
            
        }

        private void btnGenerateWordDocument_Click(object sender, EventArgs e) {

            bool success = new ReportGenerator().GenerateDocument();
            FormAMOOF.Instance.SetStatus(success == true ? "Report generated" : "Report failed to generate");
        }

        protected override void OnPaint(PaintEventArgs e) {
            base.OnPaint(e);
            GeneratePDFs();
        }
    }
}
